#jinja2: lstrip_blocks: True
{#
# Copyright 2015, Jean-Philippe Evrard <jean-philippe@evrard.me>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-#}

{% if keepalived_global_defs is defined %}
global_defs {
{% for def in keepalived_global_defs %}
  {{ def }}
{% endfor %}
}
{% endif %}

{% for name, sync_group in keepalived_sync_groups.items() %}
vrrp_sync_group {{ name }} {
  group {
    {% for instance in sync_group.instances %}
    {{ instance }}
    {% endfor %}
  }
  {% if sync_group.notify_script is defined %}
  notify "{{ sync_group.notify_script }}"
  {% endif %}
  {% if sync_group.notify_master is defined %}
  notify_master "{{ sync_group.notify_master }}"
  {% endif %}
  {% if sync_group.notify_master_rx_lower_pri is defined %}
  notify_master_rx_lower_pri "{{ sync_group.notify_master_rx_lower_pri }}"
  {% endif %}
  {% if sync_group.notify_backup is defined %}
  notify_backup "{{ sync_group.notify_backup }}"
  {% endif %}
  {% if sync_group.notify_fault is defined %}
  notify_fault "{{ sync_group.notify_fault }}"
  {% endif %}
  {% if sync_group.track_scripts is defined %}
  track_script {
    {% for track_script in sync_group.track_scripts %}
    {{ track_script }}
    {% endfor %}
  }
  {% endif %}
}
{% endfor %}

{% if keepalived_scripts is defined %}
{% for name, details in keepalived_scripts.items() %}
vrrp_script {{ name }} {
  script "{{ details.check_script }}"
  interval {{ details.interval | default(5) }}   # checking every {{ details.interval | default(5) }} seconds (default: 5 seconds)
  fall {{ details.fall | default(3) }}           # require {{ details.fall | default(3) }} failures for KO (default: 3)
  rise {{ details.rise | default(6) }}           # require {{ details.rise | default(6) }} successes for OK (default: 6)
  {% if details.weight is defined %}
  weight {{ details.weight | default(0) }}       # adjust priority by this weight, (default: 0)
  {% endif %}
  {% if details.timeout is defined %}
  timeout {{ details.timeout }}                  # allow scripts like ping to succeed, before timing out
  {% endif %}
  {% if details.user is defined %}
  user {{ details.user }}                        # user/group names to run script under.
  {% endif %}
}
{% endfor %}
{% endif %}

{% for name, instance in keepalived_instances.items() %}
vrrp_instance {{ name }} {
  interface {{ instance.interface }}
  state {{ instance.state }}
  virtual_router_id {{ instance.virtual_router_id }}
  priority {{ instance.priority }}
  {% if instance.use_vmac is defined and instance.use_vmac | bool %}
  use_vmac                                         # Use virtual MAC address
  {% endif %}
  {% if instance.nopreempt is defined and instance.nopreempt | bool %}
  nopreempt                                        # Override VRRP RFC preemption default
  {% endif %}
  {% if instance.preempt_delay is defined %}
  preempt_delay {{ instance.preempt_delay }}       # Seconds after startup until preemption. 0 (default) to 1,000.
  {% endif %}
  {% if instance.advert_int is defined %}
  advert_int {{ instance.advert_int }}
  {% endif %}
  {% if instance.dont_track_primary is defined and instance.dont_track_primary | bool %}
  dont_track_primary                               # Override VRRP RFC dont_track_primary default
  {% endif %}
  {% if instance.authentication_password is defined %}
  authentication {
    auth_type PASS
    auth_pass {{instance.authentication_password}}
  }
  {% endif %}
  {% if instance.unicast_src_ip is defined and instance.unicast_peers|length > 0 %}
  unicast_src_ip {{ instance.unicast_src_ip }}
  unicast_peer {
  {% for peer in instance.unicast_peers %}
    {{ peer }}
  {% endfor %}
  }
  {% endif %}
  virtual_ipaddress {
    {% for vip in instance.vips %}
    {{ vip }}
    {% endfor %}
  }
  {% if instance.vips_excluded is defined %}
  virtual_ipaddress_excluded {
    {% for vip in instance.vips_excluded %}
    {{ vip }}
    {% endfor %}
  }
  {% endif %}
  {% if instance.virtual_routes is defined %}
  virtual_routes {
  {% for route in instance.virtual_routes %}
    {{ route }}
  {% endfor %}
  }
  {% endif %}
  {% if instance.virtual_rules is defined %}
  virtual_rules {
  {% for rule in instance.virtual_rules %}
    {{ rule }}
  {% endfor %}
  }
  {% endif %}
  {% if instance.track_scripts is defined %}
  track_script {
    {% for track_script in instance.track_scripts %}
    {{ track_script }}
    {% endfor %}
  }
  {% endif %}

  {% if instance.track_interfaces is defined %}
  track_interface {
    {% for track_interface in instance.track_interfaces %}
    {{ track_interface }}
    {% endfor %}
  }
  {% endif %}

  {% if instance.notify_script is defined %}
  notify "{{ instance.notify_script }}"
  {% endif %}
  {% if instance.notify_master is defined %}
  notify_master "{{ instance.notify_master }}"
  {% endif %}
  {% if instance.notify_master_rx_lower_pri is defined %}
  notify_master_rx_lower_pri "{{ instance.notify_master_rx_lower_pri }}"
  {% endif %}
  {% if instance.notify_backup is defined %}
  notify_backup "{{ instance.notify_backup }}"
  {% endif %}
  {% if instance.notify_fault is defined %}
  notify_fault "{{ instance.notify_fault }}"
  {% endif %}
  {% if instance.notify_stop is defined %}
  notify_stop "{{ instance.notify_stop }}"
  {% endif %}
  {% if instance.smtp_alert is defined and instance.smtp_alert | bool %}
  smtp_alert # Send email notification during state transition
  {% endif %}
}
{% endfor %}

{% if keepalived_virtual_servers is defined %}
{% for vserver in keepalived_virtual_servers %}
virtual_server {{ vserver.ip }} {{ vserver.port }} {
  {% include './templates/keepalived-virtual-server.j2' %}

  {% for rserver in vserver.real_servers %}
  real_server {{ rserver.ip }} {{ rserver.port }} {
    {% include './templates/keepalived-real-server.j2' %}
    {% include './templates/keepalived-checks.j2' %}
  }
  {% endfor %}

}
{% endfor %}
{% endif %}
{% if keepalived_virtual_server_groups is defined %}
{% for vserver_group in keepalived_virtual_server_groups %}
virtual_server_group {{ vserver_group.name }} {
  {% for vserver_group_vip in vserver_group.vips %}
  {{ vserver_group_vip.ip }} {{ vserver_group_vip.port }}
  {% endfor %}
  {% if vserver_group.fwmark is defined %}
  fwmark {{ vserver_group.fwmark }}
  {% endif %}
}
virtual_server group {{ vserver_group.name }} {
  {# set variable to prevent duplicate code for the same task #}
  {% set vserver = vserver_group %}
  {% include './templates/keepalived-virtual-server.j2' %}
  {% for rserver in vserver_group.real_servers %}
  real_server {{ rserver.ip }} {{ rserver.port }} {
    {% include './templates/keepalived-real-server.j2' %}
    {% include './templates/keepalived-checks.j2' %}
  }
  {% endfor %}
}
{% endfor %}
{% endif %}